/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thesis.utils;

import java.io.IOException;
import java.io.Writer;
import java.util.*;

public class hVelocityEstimate {

	public final int TIME_TO_REFINE = 600;

    private BusControl buscontroller;
    private LinkedList<VelocityElement> data = new LinkedList<VelocityElement>();
    private int[] routeListScore = new int[400];
    int maxTimeStamp = 0;

    
   
    public hVelocityEstimate(BusControl bc) {
        buscontroller = bc;
    }
    public hVelocityEstimate() {
    	//TODO: check
    }
  

    public void Add(int timeStamp, LinkedList<Character> busStops, short[] routeList) {
    	VelocityElement ve = new VelocityElement(timeStamp, busStops);
    	data.add(ve);
    	if (routeList.length < 50)
    		for (int i = 0; i < routeList.length; i++) {
				routeListScore[routeList[i]+200]+=1;
    	}
    	
    	if (timeStamp>maxTimeStamp)
    		maxTimeStamp=timeStamp;
    }
    
    private short findMostAddedRoute()
    {
    	int maxscore = 0;
    	int resultRouteID  = 0;
    	for (int i = 0; i < routeListScore.length; i++) {
			if(maxscore < routeListScore[i])
			{	
				resultRouteID = i;
				maxscore = routeListScore[i];
			}	
		}
    	
    	resultRouteID-=200;
    	return (short) resultRouteID;
    }

    
    public double returnVelocity(int fromTimeStamp, int toTimeStamp, Writer out ) throws IOException {
	
    	//interested busstop => 1
    		VelocityElement ve;
    		short route = findMostAddedRoute();
    		BusRoute busroute = buscontroller.getBusRoute(route);
    		
    		
	    	//calculate distance
    		int startIndex=0, endIndex=0;
    		for (int i = 0; i < data.size(); i++) {
				if (fromTimeStamp>=data.get(i).timeStamp)
					startIndex =i;
				else
					break;
			}
    		
    		for (int i = data.size()-1; i>=0; i--) {
				if (toTimeStamp<=data.get(i).timeStamp)
					endIndex = i;
				else
					break;
    		}
    		
    		double distance =0;
    		out.write("in hVE = busroute" + (int) route + "\n");
    		out.write("id1 id2 distance " + (int)data.get(startIndex).busStops.get(0) +" " + (int)data.get(endIndex).busStops.get(0) +" "+ distance +"\n");
    		distance = buscontroller.getDistanceFromStartToEnd(route, data.get(startIndex).busStops.get(0), data.get(endIndex).busStops.get(0));
    		out.write("id1 id2 distance " + (int)data.get(startIndex).busStops.get(0) +" " + (int)data.get(endIndex).busStops.get(0) +" "+ distance +"\n");
    	
    		if (distance==0)
    		{
    			distance = buscontroller.getDistanceFromStartToEnd(route, data.get(0).busStops.get(0), data.getLast().busStops.get(0));
    			distance = distance / maxTimeStamp * (toTimeStamp-fromTimeStamp);
    		}	
//    		if (data.size() < startIndex+1)
//    			return distance/(data.get(endIndex).timeStamp - 0);
//    		else
    			return distance/(data.get(endIndex).timeStamp - data.get(startIndex+1).timeStamp);
    		

    	
    }

    
    
    
    //==============
    class VelocityElement {
        int timeStamp;
        LinkedList<Character> busStops;
        LinkedList<Short> routeList;
        public VelocityElement(int timeStamp, LinkedList<Character> busStops) {
            this.timeStamp = timeStamp;
            this.busStops = busStops;
        }
        
        
    };
  
}
